#include <fstream>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
  int i;
  int j;
  int k;
  double f;
  double g;
  double h;
  double x;
  //------Problem A-------//
  ofstream A_f;
  ofstream A_g;
  ofstream A_h;
  A_f.open("A_f.txt", std::ios::out);
  A_g.open("A_g.txt", std::ios::out);
  A_h.open("A_h.txt", std::ios::out);
  for (i=1;i<=101;i++)
    {
      x=0.99+i*0.0002;
      f=pow(x,8)-8*pow(x,7)+28*pow(x,6)-56*pow(x,5)+70*pow(x,4)-56*pow(x,3)+28*pow(x,2)-8*x+1;
      g=(((((((x-8)*x+28)*x-56)*x+70)*x-56)*x+28)*x-8)*x+1;
      h=pow(x-1,8);
      A_f << f << endl;
      A_g << g << endl;
      A_h << h << endl;
    }
  A_f.close();
  A_g.close();
  A_h.close();
  
  //------Problem B-------//

  int beta=2;
  int p=3;
  int L=-1;
  int U=1;
  double ufl;
  double ofl;
  int s;
  
  ufl=pow(beta,L);
  ofl=pow(beta,U)*(beta-pow(beta,1-p));
  cout<<"UFL"<<":"<<ufl<<endl;
  cout<<"OFL"<<":"<<ofl<<endl;
  
  s=0;
  cout << "规范化浮点数将储存在pB1.txt文件中" << endl;
  cout << "非规范化浮点数将储存在pB2.txt文件中" << endl;
  ofstream pB1;
  pB1.open("pB1.txt", std::ios::out);
  for (i=-1;i<=1;i++)
    {
      for (j=0;j<=1;j++)
	{
	  for (k=0;k<=1;k++)
	    {
	      s=s+2;
	      double fpnx;
	      fpnx=(1+j*pow(2,-1)+k*pow(2,-2))*pow(2,i);
	     
	      if (!pB1.is_open())
                 return 0;
	         pB1 << fpnx <<endl;
		 pB1 << -fpnx <<endl;


	    }

	}
    }
  pB1 << 0 <<endl;
  s=s+1;
  pB1.close();
  
    cout<<"FPN全体浮点数个数 s="<<s<<endl;
    int coro_s;
    coro_s=pow(2,p)*(U-L+1)+1;
    cout<<"推论4.19计算得到全体浮点数个数 coro_s="<<coro_s<<endl;


  ofstream pB2;
  pB2.open("pB2.txt", std::ios::out);
  for (i=0;i<=1;i++)
    {
      for (j=0;j<=1;j++)
	{
	  
	      if (i!=0 || j!=0)
		{
	      double subfpnx;
	      subfpnx=(i*pow(2,-1)+j*pow(2,-2))*pow(2,-1);
	      if (!pB2.is_open())
                 return 0;
	         pB2 << subfpnx <<endl;
		 pB2 << -subfpnx <<endl;
		}

	}
    }
  pB2.close();
  
}
